﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using RuanRang.Media.Core.Internal.DataStructures;
using RuanRang.Media.Core.Internal.Interop;

namespace RuanRang.Media.Core.Internal
{
    /// <summary>
    /// VlcOutputDevices class
    /// </summary>
    public sealed class VlcAudioOutputDevices : IEnumerable<VlcAudioOutputDevice>
    {
        /// <summary>
        /// VlcOutputDevices constructor
        /// </summary>
        internal VlcAudioOutputDevices()
        {

        }

        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns></returns>
        public IEnumerator<VlcAudioOutputDevice> GetEnumerator()
        {
            if (IntropHandleManager.LibVlcHandle == IntPtr.Zero) yield break;
            var outputDeviceListInstance = LibVlcAudioInterop.libvlc_audio_output_list_get_IntPtr(IntropHandleManager.LibVlcHandle);
            if (outputDeviceListInstance == IntPtr.Zero) yield break;
#if SILVERLIGHT
                    var audioOutput = new libvlc_audio_output_t();
                    Marshal.PtrToStructure(outputDeviceListInstance, audioOutput);
#else
            var audioOutput = (libvlc_audio_output_t)Marshal.PtrToStructure(outputDeviceListInstance, typeof(libvlc_audio_output_t));
#endif
            var lst = new VlcAudioOutputDevice(audioOutput);

            yield return lst;

            while (lst.Next != null)
            {
                lst = lst.Next;
                yield return lst;
            }
            LibVlcAudioInterop.libvlc_audio_output_list_release(outputDeviceListInstance);
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}